package models

import "backend/pkg/logging"

type User struct {
	Model

	Openid     string `json:"openid"`
	Unionid    string `json:"unionid"`
	Passwd     string `json:"passwd"`
	SessionKey string `json:"session_key"`
	Nickname   string `json:"nickname"`
	Gender     int8   `json:"gender"`
	Telephone  string `json:"telephone"`
	AvatarUrl  string `gorm:"column:avatarUrl" json:"avatarUrl"`
	Country    string `json:"country"`
	Province   string `json:"province"`
	City       string `json:"city"`
	Status     int8   `json:"status"`
}

type WxUser struct {
	NickName   string `json:"nickName"`
	Gender     int8   `json:"gender"`
	Language   string `json:"language"`
	City       string `json:"city"`
	Province   string `json:"province"`
	Country    string `json:"country"`
	AvatarUrl  string `json:"avatarUrl"`
	Openid     string `json:"openid"`
	SessionKey string `json:"session_key"`
}

func GetUserById() {

}

func ExistUserByOpenid(openid string) int {
	var user User
	db.Model(&User{}).Where("openid=?", openid).First(&user)
	if user.ID > 0 {
		return user.ID
	}

	return 0
}

func AddUser(wxStruct WxUser) int {
	var user = User{
		Openid:    wxStruct.Openid,
		Nickname:  wxStruct.NickName,
		City:      wxStruct.City,
		Province:  wxStruct.Province,
		Country:   wxStruct.Country,
		AvatarUrl: wxStruct.AvatarUrl,
	}

	db.Create(&user)

	return user.ID
}

/**
	修改用户信息
 */
func WxEditUser(wxStruct WxUser) bool {
	var user = User{
		Openid:     wxStruct.Openid,
		SessionKey: wxStruct.SessionKey,
	}
	db.Model(&User{}).Where("openid=?", user.Openid).Update(&user)
	if db.RowsAffected > 0 {
		return true
	} else {
		logging.Debug(db.Error)
		return false
	}
}
